package cpr.command;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Iterator;

import cpr.DefaultCommandHandler;
import cpr.Printer;
import cpr.context.SolveContext;
import cpr.context.WorksetContext;
import cpr.data.WorksetItem;

public class CmdWorkset extends DefaultCommandHandler {
	public CmdWorkset() {
		super("ws");
	}
	public void execute(String[] params) throws Exception {
			//getCommandContext().getContext().getCurrentWorksetContext().update();
			WorksetItem root = getCommandContext().getContext().getCurrentWorksetContext().getWorksetItemRoot();
            if (root==null) {
                Printer.println("empty");
            } else {
            	root.setRootNumberOfChilds(0);
                Printer.println("Workset: "+getCommandContext().getContext().getCurrentWorksetContext().getWorksetName()+ " "+getCommandContext().getContext().getCurrentWorksetContext().getWorksetFilePath());
                printWorkItem(root,"",root);
                Printer.println("Total worksets: "+root.getRootNumberOfChilds());
            }
            
            
	}
    private String loadSolInfo(String solFileName, boolean tmp) {
        StringBuffer solInfo = new StringBuffer();
        BufferedReader br = null;
        try {
            Reader solFileNameReader = new FileReader(solFileName);
            br = new BufferedReader(solFileNameReader);
            String solStr= br.readLine();
            do{
                if (solStr.startsWith("#")) {
                    boolean ok = true;
                    if (!tmp && (solStr.startsWith("#uncovered"))) {
                        ok=false;
                    }
                    if (ok) {
                        solInfo.append(solStr+" ");
                    }
                }
                    
                solStr= br.readLine();
            } while (solStr!=null);
            
        } catch (Exception ex) {
            solInfo.append(ex.getMessage());
        } finally {
        	try {
        		br.close();
        	} catch (Exception ex) {
        		ex.printStackTrace();
        	}
        }
        return solInfo.toString();
    }
    
	private void printWorkItem(WorksetItem item, String space, WorksetItem root) {
		String s = getCommandContext().getContext().getPrintContext().printRuleUsingDict(
				item.getClusterDescriptor().getRule(), item.getClusterDescriptor().getVector());
		String sizeString = "";
		if (item.getChildSize()>0) {
			sizeString = " = " + (item.getClusterSize()-item.getChildSize()) + "+ childs:" + item.getChildSize();
		}
        WorksetContext wctx =  getCommandContext().getContext().getCurrentWorksetContext();
        
        
		SolveContext sctx = wctx.createSolveContext( wctx.getIndexOf(item));
		String solFileName = sctx.getWorksetSolutionFilePath(true);
		File solutionFile = new File(solFileName);
		boolean solTmp = false;
        String solInfo = null;
		if (solutionFile.exists()) {
			solTmp = true;
            solInfo= loadSolInfo(solFileName,true);
		}
		solFileName = sctx.getWorksetSolutionFilePath(false);
		solutionFile = new File(solFileName);
		boolean solFull = false;
		if (solutionFile.exists()) {
			solFull = true;
            solInfo= loadSolInfo(solFileName,false);
		}
		String solutionText = "";
		if (solFull) {
			solutionText = "[solved] "+solInfo;
		} else if (solTmp) {
			solutionText = "[tmp] "+solInfo;
		} else {
			solutionText = "";
		}
		
		Printer.println(space+wctx.getIndexOf(item)
				+". " +item.getClusterDescriptor().getRule().ruleNrGlobal
				+". " +item.getClusterDescriptor().getVector().toString()
				+" "+s+" "+item.getClusterSize()+sizeString+" "+solutionText);
		root.setRootNumberOfChilds(root.getRootNumberOfChilds()+1);
		
		if (item.getChlids().size()>0) {
			for (Iterator iter = item.getChlids().iterator(); iter.hasNext();) {
				WorksetItem cw = (WorksetItem) iter.next();
				printWorkItem(cw, space +"  ",root);
			}
		}
	}
} 
